<template>
  <a-modal
    title="添加物品"
    width="80%"
    :visible="visible"
    :confirmLoading="loading"
    okText="选择"
    @ok="handleChoose"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <materialType-select v-model="queryParam.MaterialTypeId" placeholder="物品类别"></materialType-select>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.Keyword" placeholder="物品名称/编码/规格" />
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询</a-button
            >
            <a-button
              style="margin-left: 8px"
              @click="
                () => {
                  this.queryParam = { UsageStatus: true }
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >重置</a-button
            >
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table
      :scroll="{ y: scroll }"
      :columns="columns"
      :rowKey="row => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{
        selectedRowKeys: selectedRowKeys,
        onChange: onSelectChange,
        type: type,
        getCheckboxProps: CheckboxProps
      }"
      :bordered="true"
      size="small"
    >
      <template slot="dataIndex" slot-scope="text, record, index">
        <span>{{ (pagination.current - 1) * pagination.pageSize + index + 1 }}</span>
      </template></a-table
    >
  </a-modal>
</template>

<script>
import MaterialTypeSelect from '../PB/MaterialTypeSelect'
const columns = [
  {
    title: '序号',
    dataIndex: 'Id',
    scopedSlots: { customRender: 'dataIndex' },
    width: '50px'
  },
  { title: '物品名称', dataIndex: 'Name' },
  { title: '物品编码', dataIndex: 'Code' },
  { title: '物品规格', dataIndex: 'Spec' }
]

export default {
  props: {
    // inType: { type: String, required: false },
    type: { type: String, default: 'checkbox', required: false },
    id: { type: String, required: false }
  },
  components: {
    MaterialTypeSelect
  },
  mounted() {},
  computed: {
    scroll() {
      return Number(document.documentElement.clientHeight) * 0.45
    }
  },
  data() {
    return {
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], //设置pageSize的可选值，页面啊可以通过下拉框进行选择
        showSizeChanger: true,
        onShowSizeChange: (current, pageSize) => (this.pageSize = pageSize)
      },
      filters: {},
      sorter: { field: 'Id', order: 'asc' },
      loading: false,
      columns,
      queryParam: { UsageStatus: true },
      selectedRowKeys: [],
      selectedRows: [],
      visible: false
    }
  },
  methods: {
    CheckboxProps(record) {
      return {
        props: {
          disabled: record.UsageStatus === false
        }
      }
    },

    IsBecodeSelect(e) {
      console.log(e)
      this.queryParam.IsBecode = e
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.getDataList()
    },
    getDataList() {
      this.loading = true
      if (this.id) {
        console.log('拥有Id', this.id)
        this.queryParam.OrderId = this.id
        this.$http
          .post('/PB/PB_Material/QueryRecDataList', {
            PageIndex: this.pagination.current,
            PageRows: this.pagination.pageSize,
            SortField: 'Code',
            SortType: 'asc',
            Search: this.queryParam,
            ...this.filters
          })
          .then(resJson => {
            this.loading = false
            this.data = resJson.Data
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          })
      } else {
        console.log('无Id', this.id)
        this.$http
          .post('/PB/PB_Material/QueryRecDataList', {
            PageIndex: this.pagination.current,
            PageRows: this.pagination.pageSize,
            SortField: 'Code',
            SortType: 'asc',
            Search: this.queryParam,
            ...this.filters
          })
          .then(resJson => {
            this.loading = false
            this.data = resJson.Data
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          })
      }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    openChoose() {
      this.selectedRowKeys = []
      this.getDataList()
      this.visible = true
    },
    handleChoose() {
      this.visible = false
      this.$emit('onChoose', this.selectedRows)
    }
  }
}
</script>
